package com.csun.cmny.provider.service.impl;


import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.dao.UacRoleActionMapper;
import com.csun.cmny.provider.model.domain.UacAction;
import com.csun.cmny.provider.model.domain.UacRoleAction;
import com.csun.cmny.provider.model.domain.UacRoleMenu;
import com.csun.cmny.provider.model.dto.group.GroupRoleDto;
import com.csun.cmny.provider.model.dto.role.RoleAssignActionDto;
import com.csun.cmny.provider.model.exceptions.UacBizException;
import com.csun.cmny.provider.service.UacRoleActionService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;

/**
 * The class Uac role action service.
 *
 * @author paascloud.net@gmail.com
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class UacRoleActionServiceImpl extends BaseService<UacRoleAction> implements UacRoleActionService {
	@Resource
	private UacRoleActionMapper uacRoleActionMapper;

	@Override
	@Transactional(readOnly = true, rollbackFor = Exception.class)
	public List<UacRoleAction> listByRoleId(Long roleId) {
		if (roleId == null) {
			throw new UacBizException(ErrorCodeEnum.UAC10012001);
		}
		UacRoleAction roleMenu = new UacRoleAction();
		roleMenu.setRoleId(roleId);
		return uacRoleActionMapper.select(roleMenu);
	}

	@Override
	public void deleteByRoleId(Long roleId) {
		if (roleId == null) {
			throw new UacBizException(ErrorCodeEnum.UAC10012001);
		}
		UacRoleAction roleMenu = new UacRoleAction();
		roleMenu.setRoleId(roleId);
		uacRoleActionMapper.delete(roleMenu);
	}

	@Override
	public void insert(Long roleId, Set<Long> actionIdList) {
		if (roleId == null) {
			throw new UacBizException(ErrorCodeEnum.UAC10012001);
		}
		UacRoleAction uacRoleAction = new UacRoleAction();
		uacRoleAction.setRoleId(roleId);
		for (Long actionId : actionIdList) {
			uacRoleAction.setActionId(actionId);
			uacRoleActionMapper.insert(uacRoleAction);
		}
	}

	@Override
	public int deleteByRoleIdList(final List<Long> roleIdList) {
		return uacRoleActionMapper.deleteByRoleIdList(roleIdList);
	}

	/**
	 * 批量删除角色功能
	 *
	 * @param roleId       角色Id
	 * @param actionIdList 功能Id列表
	 *
	 * @return 影响的行数
	 */
	@Override
	public Integer batchDelete(Long roleId, List<Long> actionIdList) {

		return uacRoleActionMapper.batchDelete(roleId, actionIdList);
	}

	/**
	 * 批量添加角色功能
	 *
	 * @param roleId       角色Id
	 * @param actionIdList 功能Id列表
	 *
	 * @return 影响的行数
	 */
	@Override
	public Integer batchSave(Long roleId, List<Long> actionIdList) {

		return uacRoleActionMapper.batchSave(roleId, actionIdList);
	}

	/**
	 * get check role list by action id
	 *
	 * @param actionId the action id
	 *
	 * @return the role id list
	 */
	@Override
	public List<Long> getCheckRoleList(Long actionId) {

		return uacRoleActionMapper.getCheckRoleList(actionId);
	}

	/**
	 * batch save action and role list
	 *
	 * @param actionId      action id
	 * @param checkRoleList check role list
	 *
	 * @return the int.
	 */
	@Override
	public Integer batchSaveActionAndRoleList(Long actionId, List<Long> checkRoleList) {

		return uacRoleActionMapper.batchSaveActionAndRoleList(actionId, checkRoleList);
	}

	/**
	 * batch delete action and role list
	 *
	 * @param actionId       the action id
	 * @param deleteRoleList the delete role list
	 *
	 * @return the int.
	 */
	@Override
	public Integer batchDeleteActionAndRoleList(Long actionId, List<Long> deleteRoleList) {

		return uacRoleActionMapper.batchDeleteActionAndRoleList(actionId, deleteRoleList);
	}

	/**
	 * query list by role id list
	 *
	 * @param roleIdList the role id list
	 *
	 * @return the list.
	 */
	@Override
	public List<UacRoleAction> queryListByRoleIdList(List<Long> roleIdList) {

		return uacRoleActionMapper.queryListByRoleIdList(roleIdList);
	}

	/**
	 * 查询功能列表
	 *
	 * @param groupRoleDto the group role dto
	 *
	 * @return the list.
	 */
	@Override
	public List<UacAction> queryRoleActionList(GroupRoleDto groupRoleDto) {

		return uacRoleActionMapper.queryRoleActionList(groupRoleDto);
	}

	/**
	 * 查询功能列表 - 包含角色选中功能状态
	 *
	 * @param groupRoleDto the group role dto
	 *
	 * @return the list.
	 */
	@Override
	public List<UacAction> queryRoleActionListWithCheck(GroupRoleDto groupRoleDto) {

		return uacRoleActionMapper.queryRoleActionListWithCheck(groupRoleDto);
	}
}
